package com.jinsilu.freight.ui.banner;

import android.view.ViewGroup;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jinsilu.freight.R;
import com.jinsilu.freight.http.glide.GlideApp;
import com.jinsilu.freight.http.response.ADBean;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;
import com.youth.banner.util.LogUtils;

import java.util.List;

/**
 * 自定义布局，图片
 */
public class ImageAdapter extends BannerAdapter<ADBean, ImageHolder> {

    public ImageAdapter(List<ADBean> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
    }

    //更新数据
    public void updateData(List<ADBean> data) {
        //这里的代码自己发挥，比如如下的写法等等
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }


    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
        ConstraintLayout constraintLayout = (ConstraintLayout) BannerUtils.getView(parent, R.layout.banner_image);
        return new ImageHolder(constraintLayout);
    }

    @Override
    public void onBindView(ImageHolder holder, ADBean data, int position, int size) {
        //通过图片加载器实现圆角，你也可以自己使用圆角的imageview，实现圆角的方法很多，自己尝试哈
        GlideApp.with(holder.imageView)
                .load(data.getImageRes())
                .placeholder(R.drawable.ic_default_details_placeholder)
                .error(R.drawable.ic_default_details_placeholder)
//                .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(4)))
                .into(holder.imageView);
    }
}
